﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变.user_Form
{
    public partial class user_Violation : Form
    {
        public user_Violation()
        {
            InitializeComponent();
        }
        public string opname { get; set; }
        private void ucDataGridView1_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "RId", HeadText = "编号", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "carname", HeadText = "车名", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "carid", HeadText = "车牌号", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "reason", HeadText = "原因", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "pay", HeadText = "罚金", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "checkName", HeadText = "检测人", Width = 60, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "checkTime", HeadText = "检测时间", Width = 80, WidthType = SizeType.Absolute, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd HH:mm:ss"); } });
            this.ucDataGridView1.Columns = lstCulumns;
            this.ucDataGridView1.IsShowCheckBox = true;

            string sql = " select * from backTable where username=@username and isViolation ='True' and isPay='False'";
            SqlParameter p = new SqlParameter("@username",this.opname);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql, p);
            this.ucDataGridView1.DataSource = dt;
            this.ucDataGridView1.First();
        }

        private void user_Violation_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
        }

        private void btnpay_Click(object sender, EventArgs e)
        {
            //点击支付，默认使用余额扣款，
            DialogResult dir = MessageBox.Show("您确定使用余额支付吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dir == DialogResult.OK)
            {
                string sql3 = "select umoney from uUser where uUsername=@uUsername";
                SqlParameter p = new SqlParameter("@uUsername", opname);
                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery(sql3, p);
                //获得余额
                string Mon = dt.Rows[0][0].ToString();
                string price = (ucDataGridView1.SelectRow.DataSource as DataRow)["pay"].ToString();
                //拿余额与需要支付的金额进行比较
                if (Convert.ToDecimal(Mon) >= Convert.ToDecimal(price))
                {
                    //余额支付
                   string id = (ucDataGridView1.SelectRow.DataSource as DataRow)["RId"].ToString();
                    string opername = (ucDataGridView1.SelectRow.DataSource as DataRow)["username"].ToString();
                    string sql = "update uUser set umoney = umoney-@price where uUsername=@uUsername";
                    SqlParameter[] ps =
                    {
                    new SqlParameter("@price",price),
                    new SqlParameter("@uUsername",opername)
                };
                    int result = DBHelper.ExecuteNonQuery(sql, ps);
                    if (result > 0)
                    {
                        //支付成功，扣掉余额,修改支付状态
                        this.ShowSuccessTip("罚款支付成功，请规范用车！");
                        string sql2 = "update backTable set isPay='True'  where RId = @RentId";
                        SqlParameter[] ps2 =
                        {
                        new SqlParameter("@RentId",id),
                        new SqlParameter("@Paytime",DateTime.Now)
                };

                        DBHelper.ExecuteNonQuery(sql2, ps2);
                        ucDataGridView1_Load(null, null);
                    }
                }


                else
                {
                    this.ShowErrorDialog("余额不足!请充值！");
                }
            }
            else
            {
                return;
            }
        }
    }
}
